<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style media="screen">
        * {
            margin: 0;
            padding: 0;
        }

        #box {
            width: 800px;
            height: 600px;
            position: relative;
            margin: 100px auto;
            background: #000000;
            border: 2px solid red;
            overflow: hidden;
        }

        .ball {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            position: absolute;
            bottom: 0;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        function FireWork() {
            this.container = document.querySelector('#box')
            this.bindEvent()
        }
        FireWork.prototype.bindEvent = function () {
            var self = this;
            self.container.onclick = function (e) {
                e = e || window.event;
                var fireWork = self.createFireWork()
                fireWork.style.left = e.offsetX + 'px';
                $(fireWork).animate({
                    top: e.offsetY,

                }, 1000, function () {
                    fireWork.remove()
                    self.boom(e.offsetX, e.offsetY)
                })

            }
        }
        FireWork.prototype.createFireWork = function () {
            var div = document.createElement('div')
            div.className = 'ball'
            div.style.backgroundColor = '#' + Math.round(parseInt('ffffff', '16') * Math.random()).toString(16).padStart(6, '0')
            this.container.appendChild(div)
            return div
        }
        FireWork.prototype.boom = function (x, y) {
            var deg = 0;
            var r = 100;
            for (var i = 0; i < 30; i++) {
                var fire = this.createFireWork();
                fire.style.left = x + 'px';
                fire.style.top = y + 'px';
                deg += 12;
                var target_x = Math.round(Math.cos(Math.PI / 180 * deg) * r)
                var target_y = Math.round(Math.sin(Math.PI / 180 * deg) * r)
                $(fire).animate({
                    left: x + target_x,
                    top: y + target_y
                }, 1000,
                    function () {
                        this.remove()
                    }
                )
            }
        }
        FireWork.prototype.autoFire = function () {
            var self = this;
            setInterval(function () {
                var e = {
                    offsetX: Math.round(Math.random() * self.container.offsetWidth) + 100,
                    offsetY: Math.round(Math.random() * self.container.offsetHeight) + 100
                }
                self.container.onclick(e)
            }, 1000)
        }
        var fw = new FireWork()
        fw.autoFire()
    </script>
</body>

</html>